home *** CD-ROM | disk | FTP | other *** search
- Path: wormer.fn.net!sysadmin@wormer.fn.net
- From: demstar@fn.net (Invalid Opcode)
- Newsgroups: comp.lang.c
- Subject: Re: Recursion
- Date: Sun, 07 Apr 1996 20:10:12 GMT
- Organization: Feist Connections
- Message-ID: <4k93to$nsf@wormer.fn.net>
- References: <31624BC2.70D2@sooner.net> <4k0nlv$hn6@linet06.li.net>
- NNTP-Posting-Host: mark313.fn.net
- X-Newsreader: Forte Agent .99b.112
-
- Eddie Bush (edwbush@sooner.net) wrote:
-
- > I am trying to construct a C function that will recursively convert
- > a string such as "1234" into it's integer equivelant (1234).
- > 2)the function should be called with a character pointer:
- > Such as: convert("1234");
- > making the prototype look something like:
- > int convert(char *p);
-
- Uhhhhhhhhhhhh....
-
- #include <stdio.h>
-
- unsigned sztou(char *s);
-
- int main(void)
- {
- char *num = { "16161" };
-
- printf("\n%u", sztou(num));
-
- return (0);
- }
-
- unsigned sztou(char *s)
- {
- static unsigned n = 1;
- unsigned val;
-
- if (*s)
- {
- val = sztou(s + 1) + (*s - '0') * n;
- n *= 10;
- return (val);
- }
-
- return(0);
- }
-
- ????
-
- That's the unsigned int, no error checking version. There are tons of
- bad things that could happen with this, so it's by no means acceptable
- as anything but an answer to a teaching exercise. I tried to think of
- a way to do sztou without using the temporary val variable, but
- nothing (that worked) came to mind. Pffft. Oh well. I'm glad I'm
- not in an Intro to C class. :)
-
-